package com.wx.controller.verifyController;

import com.wx.entity.WxUserModel;
import com.wx.pojo.JSONResult;
import com.wx.pojo.WxUser;
import com.wx.service.ICollectService;
import com.wx.service.IUserProductDetailService;
import com.wx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/verify")
public class VerifyController extends WxUserModel {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IUserProductDetailService iUserProductDetailService;
    @Autowired
    private ICollectService iCollectService;

    @GetMapping(value = "/listUser")
    public String listUser(ModelMap map){

        map.addAttribute("listUser",iUserService.listUser());
        for (WxUser user:iUserService.listUser()) {
            System.out.println(user.getUsername());
        }
        return "userView/listUser";
    }

    /**
     * 我的集采--列表
     * @param map
     * @param user_id
     * @param status
     * @param request
     * @return
     */
    @GetMapping("/listCollect")
    public String listCollect(ModelMap map,@RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id, Integer status,HttpServletRequest request){
        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }

        //我的集采列表
        map.addAttribute("listCollect",iUserProductDetailService.listModelDetail(user_id,1,9));

        map.addAttribute("listCollected",iUserProductDetailService.listModelDetail(user_id,2,1));

        return "verifyView/listCollect";
    }

    /**
     * 我的集采--集采详情
     * @param map
     * @param collect_number 集采活动编号
     * @param user_id  用户ID
     * @param request
     * @return
     */
    @GetMapping("/getCollectDetail")
    public String getCollectDetail(ModelMap map, @RequestParam(value = "collect_number",defaultValue = "101547722416395") String collect_number, @RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id, HttpServletRequest request){
        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }

        //获取集采详情
        map.addAttribute("collectModel",iUserProductDetailService.getModelDetail(collect_number,user_id));

        //展示参与集采用户列表
        map.addAttribute("list",iUserProductDetailService.listModelJoinUser(collect_number));
        return "verifyView/collectDetail";
    }

    /**
     * 参加集采活动
     * @param map
     * @param collect_number 集采活动编号
     * @param user_id 参与用户
     * @param request
     * @return
     */
    @GetMapping("/getCollectGoing")
    public String getCollectGoing(ModelMap map,
                                  @RequestParam(value = "collect_number",defaultValue = "101547722416395") String collect_number,
                                  @RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id,
                                  HttpServletRequest request){

        //获取用户id
        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }

        //判断用户是否已经参与集采活动 已经参与直接返回
//        if (iUserProductDetailService.listDistinctModel(collect_number,user_id,null).get(0).getAmount()>0){
//            return "redirect:/";
//        }

        //参与集采 生成用户购买明细记录明细
        iUserProductDetailService.participateModel(user_id,collect_number);

        //展示 用户参与集采活动详情 （未生成订单）
        map.addAttribute("collectModel",iUserProductDetailService.getModelDetail(collect_number,user_id));

        //展示 集采活动参与的用户的列表
        map.addAttribute("list",iUserProductDetailService.listModelJoinUser(collect_number));
        return "verifyView/collectGoing";
    }

    @GetMapping("/joinCollect")
    @ResponseBody
    public String joinCollect(Model model,
                              @RequestParam(value = "item",defaultValue = " ") String item,
                              @RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id,
                              @RequestParam(value = "depot_id",defaultValue ="1" ) Integer depot_id,
                              @RequestParam(value = "logistics",defaultValue ="1" ) Integer logistics,
                              @RequestParam(value = "number",defaultValue ="101547722416395" ) String number,
                              HttpServletRequest request) {
        System.out.println(item+logistics+number);
        if (item.equals(" ")){
            item="1,66;2,88;";
        }
        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }
        //修改集采活动 总吨数，油库 提油方式 以及用户买油明细的吨数
        iCollectService.joinCollect(item,number,user_id,depot_id,logistics);
//            model.addAttribute("joinModel","参与成功！");

         return "参与成功！";
    }

    @GetMapping("joinSuccess")
    public String joinSuccess(Model model){
        model.addAttribute("joinModel","参与成功！");
        return "joinSuccess";
    }

    /**
     * 我的订单--列表
     * @param map
     * @param user_id
     * @param status
     * @param request
     * @return
     */
    @GetMapping("/listOrder")
    public String listOrder(ModelMap map,@RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id, Integer status,HttpServletRequest request){
        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }

        //我的订单列表
        map.addAttribute("listOrder",iUserProductDetailService.listModelDetail(user_id,1,2));
        map.addAttribute("listOrdered",iUserProductDetailService.listModelDetail(user_id,2,2));

        return "verifyView/listOrder";
    }

    /**
     * 我的订单--订单详情
     * @param map
     * @param collect_number 集采活动编号
     * @param user_id  用户ID
     * @param request
     * @return
     */
    @GetMapping("/getOrderDetail")
    public String getOrderDetail(ModelMap map,
                                 @RequestParam(value = "collect_number",defaultValue = "101547722416395") String collect_number,
                                 @RequestParam(value = "user_id",defaultValue ="3" ) Integer user_id,
                                 HttpServletRequest request){

        if (getUserModel(request).getId()!=null){
            user_id=getUserModel(request).getId();
        }

        //获取集采详情
        map.addAttribute("orderModel",iUserProductDetailService.getModelDetail(collect_number,user_id));

        return "verifyView/orderDetail";
    }




}
